package com.lxs.mall.member.service.impl;

import com.lxs.mall.member.entity.MemberLevelEntity;
import com.lxs.mall.member.exception.PhoneException;
import com.lxs.mall.member.exception.UsernameException;
import com.lxs.mall.member.service.MemberLevelService;
import com.lxs.mall.member.vo.MemberLoginVO;
import com.lxs.mall.member.vo.MemberRegisterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxs.common.utils.PageUtils;
import com.lxs.common.utils.Query;

import com.lxs.mall.member.dao.MemberDao;
import com.lxs.mall.member.entity.MemberEntity;
import com.lxs.mall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    MemberLevelService memberLevelService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void register(MemberRegisterVO vo) throws UsernameException,PhoneException {
        MemberEntity memberEntity = new MemberEntity();
        //设置默认会员等级
        MemberLevelEntity levelEntity=memberLevelService.queryMemberLevelDefault();
        memberEntity.setLevelId(levelEntity.getId());
        //校验账号和手机号是不能重复的
        checkUniqueUsername(vo.getUsername());
        //增加账号
        memberEntity.setUsername(vo.getUsername());
        //校验手机号
        checkUniquePhone(vo.getPhone());
        //增加手机号
        memberEntity.setMobile(vo.getPhone());
        //密码加密处理
        BCryptPasswordEncoder bCryptPasswordEncoder= new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(vo.getPassword());
        memberEntity.setPassword(encode);
        //设置其他默认值
        memberEntity.setNickname(vo.getUsername());
        //保存
        this.save(memberEntity);

    }

    @Override
    public MemberEntity login(MemberLoginVO vo) {
        // 1.根据账号或者手机号来查询会员信息
        MemberEntity entity = this.getOne(new QueryWrapper<MemberEntity>()
                .eq("username", vo.getUserName())
                .or()
                .eq("mobile", vo.getUserName()));
        if(entity != null){
            // 2.如果账号或者手机号存在 然后根据密码加密后的校验来判断是否登录成功
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            boolean matches = encoder.matches(vo.getPassword(), entity.getPassword());
            if(matches){
                // 表明登录成功
                return entity;
            }
        }
        return null;
    }

    private void checkUniqueUsername(String username) {
        int i = (int) this.count(new QueryWrapper<MemberEntity>().eq("username",username));
        if(i>0){
            throw new UsernameException();
        }
    }

    private void checkUniquePhone(String phone) {
        int i = (int) this.count(new QueryWrapper<MemberEntity>().eq("mobile",phone));
        if(i>0){
            throw new PhoneException();
        }
    }

}
